In this article, I will show you how to convert html to pdf in c# using iTextSharp. For that, you need to Download the iTextSharp PDF library and unzip. Copy and paste the following dlls itextsharp and itextsharp.xmlworker in the project folder and reference it .
Step 1: Create a asp.net project and Create a new aspx page and name it as Default.aspx. Copy and paste the HTML markup code in the design page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" ValidateRequest="false" Inherits="MymvcApp.PrintData" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div id="Content">
<table cellspacing="0" cellpadding="2" style="border-collapse: collapse; border: 1px solid #2595de; font-size: 11pt;">
<tr>
<th style="background-color: #B8DBFD; border: 1px solid #2595de">Employee ID</th>
<th style="background-color: #B8DBFD; border: 1px solid #2595de">Name</th>
<th style="background-color: #B8DBFD; border: 1px solid #2595de">City</th>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #2595de">111</td>
<td style="width: 150px; border: 1px solid #2595de">Mohamed Mohideen</td>
<td style="width: 120px; border: 1px solid #2595de">Chennai</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #2595de">112</td>
<td style="width: 150px; border: 1px solid #2595de">Manzoor</td>
<td style="width: 120px; border: 1px solid #2595de">Mumbai</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #2595de">113</td>
<td style="width: 150px; border: 1px solid #2595de">Thivan Mydeen</td>
<td style="width: 120px; border: 1px solid #2595de">Banglore</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #2595de">114</td>
<td style="width: 150px; border: 1px solid #2595de">Mohamed Rasik</td>
<td style="width: 120px; border: 1px solid #2595de">Tirunelveli</td>
</tr>
</table>
</div>
<br />
<asp:HiddenField ID="hfdContent" runat="server" />
<asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="ExportToPDF" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnExport").click(function () {
$("#hfdContent").val($("#Content").html());
});
});
</script>
</body>
</html>
Step 2: Press F7 ,Copy and paste the following code behind Default.aspx.cs.
protected void ExportToPDF(object sender, EventArgs e)
{
Guid newID = Guid.NewGuid();
StringReader sr = new StringReader(Request.Form[hfdContent.UniqueID]);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + newID + "HTML.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
Description: When the button is clicked, The Html string value is extracted from the Hidden field. A new document will be created with formatted and convert Html to PDF in c# using itextsharp. The file name for the pdf file is generated as guid and the PDF is downloaded in the browser.
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article